/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "zobjects_gtk.h"

ZlangInvokeFunction ZlangInvokeFunction_guicalendar_Create_guiwindow;
int ZlangInvokeFunction_guicalendar_Create_guiwindow( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	struct ZlangDirectProperty_guicalendar		*guicalendar_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guicalendar_common = & (guicalendar_direct_prop->common) ;

	EVAL_GTK_WIDGET_NEW_IN_CREATE( guicalendar_common->gtk_widget , gtk_calendar_new() )
	SET_CONTROL_DEFAULT_STYLE( in1 , guicalendar_common->gtk_widget )

	TRANSFER_INPUT_OBJECT_TO_NEW_OBJECT( guicalendar_common->guiwindow_obj , in1 )

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guicalendar_Create_guiwindow_int_int_int;
int ZlangInvokeFunction_guicalendar_Create_guiwindow_int_int_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	struct ZlangObject				*in4 = GetInputParameterInLocalObjectStack(rt,4) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	struct ZlangDirectProperty_guicalendar		*guicalendar_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guicalendar_common = & (guicalendar_direct_prop->common) ;
	int32_t						year ;
	int32_t						month ;
	int32_t						day ;

	CallRuntimeFunction_int_GetIntValue( rt , in2 , & year );
	CallRuntimeFunction_int_GetIntValue( rt , in3 , & month );
	CallRuntimeFunction_int_GetIntValue( rt , in4 , & day );

	EVAL_GTK_WIDGET_NEW_IN_CREATE( guicalendar_common->gtk_widget , gtk_calendar_new() )
	SET_CONTROL_DEFAULT_STYLE( in1 , guicalendar_common->gtk_widget )
	gtk_calendar_set_year( GTK_CALENDAR(guicalendar_common->gtk_widget) , year );
	gtk_calendar_set_month( GTK_CALENDAR(guicalendar_common->gtk_widget) , month );
	gtk_calendar_set_day( GTK_CALENDAR(guicalendar_common->gtk_widget) , day );

	TRANSFER_INPUT_OBJECT_TO_NEW_OBJECT( guicalendar_common->guiwindow_obj , in1 )

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guicalendar_Create_guiwindow_string;
int ZlangInvokeFunction_guicalendar_Create_guiwindow_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char						*datetime_str = NULL ;
	struct ZlangDirectProperty_guicalendar		*guicalendar_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guicalendar_common = & (guicalendar_direct_prop->common) ;
	int32_t						year ;
	int32_t						month ;
	int32_t						day ;
	int						nret = 0 ;

	CallRuntimeFunction_string_GetStringValue( rt , in2 , & datetime_str , NULL );

	EVAL_GTK_WIDGET_NEW_IN_CREATE( guicalendar_common->gtk_widget , gtk_calendar_new() )
	SET_CONTROL_DEFAULT_STYLE( in1 , guicalendar_common->gtk_widget )
	nret = sscanf( datetime_str , "%d-%d-%d" , & year , & month , & day ) ;
	if( nret != 3 )
	{
		UnreferObject( rt , out1 );
		return 0;
	}
	gtk_calendar_set_year( GTK_CALENDAR(guicalendar_common->gtk_widget) , year );
	gtk_calendar_set_month( GTK_CALENDAR(guicalendar_common->gtk_widget) , month-1 );
	gtk_calendar_set_day( GTK_CALENDAR(guicalendar_common->gtk_widget) , day );

	TRANSFER_INPUT_OBJECT_TO_NEW_OBJECT( guicalendar_common->guiwindow_obj , in1 )

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guicalendar_SetDate_string;
int ZlangInvokeFunction_guicalendar_SetDate_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guicalendar		*guicalendar_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guicalendar_common = & (guicalendar_direct_prop->common) ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char						*datetime_str = NULL ;
	int32_t						year ;
	int32_t						month ;
	int32_t						day ;
	int						nret = 0 ;

	CallRuntimeFunction_string_GetStringValue( rt , in1 , & datetime_str , NULL );

	nret = sscanf( datetime_str , "%d-%d-%d" , & year , & month , & day ) ;
	if( nret != 3 )
	{
		UnreferObject( rt , out1 );
		return 0;
	}
	gtk_calendar_set_year( GTK_CALENDAR(guicalendar_common->gtk_widget) , year );
	gtk_calendar_set_month( GTK_CALENDAR(guicalendar_common->gtk_widget) , month-1 );
	gtk_calendar_set_day( GTK_CALENDAR(guicalendar_common->gtk_widget) , day );

	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guicalendar_SetDate_int_int_int;
int ZlangInvokeFunction_guicalendar_SetDate_int_int_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guicalendar		*guicalendar_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guicalendar_common = & (guicalendar_direct_prop->common) ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						year ;
	int32_t						month ;
	int32_t						day ;

	CallRuntimeFunction_int_GetIntValue( rt , in1 , & year );
	CallRuntimeFunction_int_GetIntValue( rt , in2 , & month );
	CallRuntimeFunction_int_GetIntValue( rt , in3 , & day );

	gtk_calendar_set_year( GTK_CALENDAR(guicalendar_common->gtk_widget) , year );
	gtk_calendar_set_month( GTK_CALENDAR(guicalendar_common->gtk_widget) , month-1 );
	gtk_calendar_set_day( GTK_CALENDAR(guicalendar_common->gtk_widget) , day );

	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guibutton_SetDaySelectedEventCallback_functionptr;
int ZlangInvokeFunction_guibutton_SetDaySelectedEventCallback_functionptr( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guibutton		*guibutton_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guibutton_common = & (guibutton_direct_prop->common) ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;

	ADD_CONTROL_GENERAL_CALLBACK_IN_CREATE( guibutton_common->guiwindow_obj , obj , GTK_EVENT_DAY_SELECTED , in1 )

	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT

	return 0;
}

ZLANG_INVOKE_FUNCTION_guiwidget_all_common_functions( calendar )

ZLANG_INVOKE_FUNCTION_guictl_all_common_functions( calendar )

ZlangInvokeFunction ZlangInvokeFunction_guicalendar_GetDate;
int ZlangInvokeFunction_guicalendar_GetDate( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guicalendar		*guicalendar_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guicalendar_common = & (guicalendar_direct_prop->common) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						year ;
	int32_t						month ;
	int32_t						day ;
	char						datetime_str[ 4+1+2+1+2 + 1 ] = "" ;

	year = gtk_calendar_get_year( GTK_CALENDAR(guicalendar_common->gtk_widget) ) ;
	month = gtk_calendar_get_month( GTK_CALENDAR(guicalendar_common->gtk_widget) ) ;
	day = gtk_calendar_get_day( GTK_CALENDAR(guicalendar_common->gtk_widget) ) ;
	snprintf( datetime_str , sizeof(datetime_str) , "%04d-%02d-%02d" , year , month+1 , day );
	CallRuntimeFunction_string_SetStringValue( rt , out1 , datetime_str , STRLEN_OF_STRING );

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guicalendar_GetYear;
int ZlangInvokeFunction_guicalendar_GetYear( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guicalendar		*guicalendar_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guicalendar_common = & (guicalendar_direct_prop->common) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						year ;

	year = gtk_calendar_get_year( GTK_CALENDAR(guicalendar_common->gtk_widget) ) ;
	CallRuntimeFunction_int_SetIntValue( rt , out1 , year );

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guicalendar_GetMonth;
int ZlangInvokeFunction_guicalendar_GetMonth( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guicalendar		*guicalendar_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guicalendar_common = & (guicalendar_direct_prop->common) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						month ;

	month = gtk_calendar_get_month( GTK_CALENDAR(guicalendar_common->gtk_widget) ) ;
	CallRuntimeFunction_int_SetIntValue( rt , out1 , month+1 );

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guicalendar_GetDay;
int ZlangInvokeFunction_guicalendar_GetDay( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guicalendar		*guicalendar_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guicalendar_common = & (guicalendar_direct_prop->common) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						day ;

	day = gtk_calendar_get_day( GTK_CALENDAR(guicalendar_common->gtk_widget) ) ;
	CallRuntimeFunction_int_SetIntValue( rt , out1 , day );

	return 0;
}

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_guicalendar;
void *ZlangCreateDirectProperty_guicalendar( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guicalendar		*guicalendar_prop = NULL ;
	struct ZlangDirectProperty_guiwidget_common	*guicalendar_common = NULL ;
	
	guicalendar_prop = (struct ZlangDirectProperty_guicalendar *)ZLMALLOC( sizeof(struct ZlangDirectProperty_guicalendar) ) ;
	if( guicalendar_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( guicalendar_prop , 0x00 , sizeof(struct ZlangDirectProperty_guicalendar) );

	guicalendar_common = (struct ZlangDirectProperty_guiwidget_common *)guicalendar_prop ;
	guicalendar_common->gtk_widget_type_id = GTK_TYPE_LABEL ;

	INIT_ZLANGDIRECTPROPERTY_guiwidget_common(guicalendar)
	
	return guicalendar_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_guicalendar;
void ZlangDestroyDirectProperty_guicalendar( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guicalendar	*guicalendar_direct_prop = GetObjectDirectProperty(obj) ;
	
	CLEAN_ZLANGDIRECTPROPERTY_guiwidget_common(guicalendar)

	ZLFREE( guicalendar_direct_prop );
	
	return;
}

ZlangSummarizeDirectPropertySizeFunction ZlangSummarizeDirectPropertySize_guicalendar;
void ZlangSummarizeDirectPropertySize_guicalendar( struct ZlangRuntime *rt , struct ZlangObject *obj , size_t *summarized_obj_size , size_t *summarized_direct_prop_size )
{
	SUMMARIZE_SIZE( summarized_direct_prop_size , sizeof(struct ZlangDirectProperty_guicalendar) )
	return;
}

static struct ZlangDirectFunctions direct_funcs_guicalendar =
	{
		ZLANG_OBJECT_guicalendar , /* char *ancestor_name */
		
		ZlangCreateDirectProperty_guicalendar , /* ZlangCreateDirectPropertyFunction *create_entity_func */
		ZlangDestroyDirectProperty_guicalendar , /* ZlangDestroyDirectPropertyFunction *destroy_entity_func */
		
		NULL , /* ZlangFromCharPtrFunction *from_char_ptr_func */
		NULL , /* ZlangToStringFunction *to_string_func */
		NULL , /* ZlangFromDataPtrFunction *from_data_ptr_func */
		NULL , /* ZlangGetDataPtrFunction *get_data_ptr_func */
		
		NULL , /* ZlangOperatorFunction *oper_PLUS_func */
		NULL , /* ZlangOperatorFunction *oper_MINUS_func */
		NULL , /* ZlangOperatorFunction *oper_MUL_func */
		NULL , /* ZlangOperatorFunction *oper_DIV_func */
		NULL , /* ZlangOperatorFunction *oper_MOD_func */
		
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NEGATIVE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NOT_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_BIT_REVERSE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_PLUS_PLUS_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_MINUS_MINUS_func */
		
		NULL , /* ZlangCompareFunction *comp_EGUAL_func */
		NULL , /* ZlangCompareFunction *comp_NOTEGUAL_func */
		NULL , /* ZlangCompareFunction *comp_LT_func */
		NULL , /* ZlangCompareFunction *comp_LE_func */
		NULL , /* ZlangCompareFunction *comp_GT_func */
		NULL , /* ZlangCompareFunction *comp_GE_func */
		
		NULL , /* ZlangLogicFunction *logic_AND_func */
		NULL , /* ZlangLogicFunction *logic_OR_func */
		
		NULL , /* ZlangLogicFunction *bit_AND_func */
		NULL , /* ZlangLogicFunction *bit_XOR_func */
		NULL , /* ZlangLogicFunction *bit_OR_func */
		NULL , /* ZlangLogicFunction *bit_MOVELEFT_func */
		NULL , /* ZlangLogicFunction *bit_MOVERIGHT_func */
		
		ZlangSummarizeDirectPropertySize_guicalendar , /* ZlangSummarizeDirectPropertySizeFunction *summarize_direct_prop_size_func */
	} ;

ZlangImportObjectFunction ZlangImportObject_guicalendar;
struct ZlangObject *ZlangImportObject_guicalendar( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangObject	*prop = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_guicalendar , & direct_funcs_guicalendar , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	prop = AddPropertyInObject( rt , obj , GetFloatObjectInRuntimeObjectsHeap(rt) , "ALIGN_LEFT" ) ;
	if( prop == NULL )
		return NULL;
	CallRuntimeFunction_float_SetFloatValue( rt , prop , 0.0 );
	SetConstantObject( prop );
	
	prop = AddPropertyInObject( rt , obj , GetFloatObjectInRuntimeObjectsHeap(rt) , "ALIGN_CENTER" ) ;
	if( prop == NULL )
		return NULL;
	CallRuntimeFunction_float_SetFloatValue( rt , prop , 0.5 );
	SetConstantObject( prop );
	
	prop = AddPropertyInObject( rt , obj , GetFloatObjectInRuntimeObjectsHeap(rt) , "ALIGN_RIGHT" ) ;
	if( prop == NULL )
		return NULL;
	CallRuntimeFunction_float_SetFloatValue( rt , prop , 1.0 );
	SetConstantObject( prop );
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guicalendar_Create_guiwindow , ZLANG_OBJECT_guicalendar , "Create" , ZLANG_OBJECT_guiwindow,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guicalendar_Create_guiwindow_string , ZLANG_OBJECT_guicalendar , "Create" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guicalendar_Create_guiwindow_int_int_int , ZLANG_OBJECT_guicalendar , "Create" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guicalendar_SetDate_string , ZLANG_OBJECT_guicalendar , "SetDate" , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guicalendar_SetDate_int_int_int , ZLANG_OBJECT_guicalendar , "SetDate" , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guibutton_SetDaySelectedEventCallback_functionptr , ZLANG_OBJECT_guicalendar , "SetDaySelectedEventCallback" , ZLANG_OBJECT_functionptr,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guiwidget_all_common_functions( calendar )

	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guictl_all_common_functions( calendar )

	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guicalendar_GetDate , ZLANG_OBJECT_string , "GetDate" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guicalendar_GetYear , ZLANG_OBJECT_int , "GetYear" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guicalendar_GetMonth , ZLANG_OBJECT_int , "GetMonth" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guicalendar_GetDay , ZLANG_OBJECT_int , "GetDay" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	return obj ;
}

